{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 1.15 \u901a\u8fc7\u67d0\u4e2a\u5b57\u6bb5\u5c06\u8bb0\u5f55\u5206\u7ec4\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u6709\u4e00\u4e2a\u5b57\u5178\u6216\u8005\u5b9e\u4f8b\u7684\u5e8f\u5217\uff0c\u7136\u540e\u4f60\u60f3\u6839\u636e\u67d0\u4e2a\u7279\u5b9a\u7684\u5b57\u6bb5\u6bd4\u5982 date \u6765\u5206\u7ec4\u8fed\u4ee3\u8bbf\u95ee\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "itertools.groupby() \u51fd\u6570\u5bf9\u4e8e\u8fd9\u6837\u7684\u6570\u636e\u5206\u7ec4\u64cd\u4f5c\u975e\u5e38\u5b9e\u7528\u3002\n\u4e3a\u4e86\u6f14\u793a\uff0c\u5047\u8bbe\u4f60\u5df2\u7ecf\u6709\u4e86\u4e0b\u5217\u7684\u5b57\u5178\u5217\u8868\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "rows = [\n    {'address': '5412 N CLARK', 'date': '07/01/2012'},\n    {'address': '5148 N CLARK', 'date': '07/04/2012'},\n    {'address': '5800 E 58TH', 'date': '07/02/2012'},\n    {'address': '2122 N CLARK', 'date': '07/03/2012'},\n    {'address': '5645 N RAVENSWOOD', 'date': '07/02/2012'},\n    {'address': '1060 W ADDISON', 'date': '07/02/2012'},\n    {'address': '4801 N BROADWAY', 'date': '07/01/2012'},\n    {'address': '1039 W GRANVILLE', 'date': '07/04/2012'},\n]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u73b0\u5728\u5047\u8bbe\u4f60\u60f3\u5728\u6309 date \u5206\u7ec4\u540e\u7684\u6570\u636e\u5757\u4e0a\u8fdb\u884c\u8fed\u4ee3\u3002\u4e3a\u4e86\u8fd9\u6837\u505a\uff0c\u4f60\u9996\u5148\u9700\u8981\u6309\u7167\u6307\u5b9a\u7684\u5b57\u6bb5(\u8fd9\u91cc\u5c31\u662f date )\u6392\u5e8f\uff0c\n\u7136\u540e\u8c03\u7528 itertools.groupby() \u51fd\u6570\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from operator import itemgetter\nfrom itertools import groupby\n\n# Sort by the desired field first\nrows.sort(key=itemgetter('date'))\n# Iterate in groups\nfor date, items in groupby(rows, key=itemgetter('date')):\n    print(date)\n    for i in items:\n        print(' ', i)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd0\u884c\u7ed3\u679c\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "07/01/2012\n  {'date': '07/01/2012', 'address': '5412 N CLARK'}\n  {'date': '07/01/2012', 'address': '4801 N BROADWAY'}\n07/02/2012\n  {'date': '07/02/2012', 'address': '5800 E 58TH'}\n  {'date': '07/02/2012', 'address': '5645 N RAVENSWOOD'}\n  {'date': '07/02/2012', 'address': '1060 W ADDISON'}\n07/03/2012\n  {'date': '07/03/2012', 'address': '2122 N CLARK'}\n07/04/2012\n  {'date': '07/04/2012', 'address': '5148 N CLARK'}\n  {'date': '07/04/2012', 'address': '1039 W GRANVILLE'}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "groupby() \u51fd\u6570\u626b\u63cf\u6574\u4e2a\u5e8f\u5217\u5e76\u4e14\u67e5\u627e\u8fde\u7eed\u76f8\u540c\u503c\uff08\u6216\u8005\u6839\u636e\u6307\u5b9a key \u51fd\u6570\u8fd4\u56de\u503c\u76f8\u540c\uff09\u7684\u5143\u7d20\u5e8f\u5217\u3002\n\u5728\u6bcf\u6b21\u8fed\u4ee3\u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u503c\u548c\u4e00\u4e2a\u8fed\u4ee3\u5668\u5bf9\u8c61\uff0c\n\u8fd9\u4e2a\u8fed\u4ee3\u5668\u5bf9\u8c61\u53ef\u4ee5\u751f\u6210\u5143\u7d20\u503c\u5168\u90e8\u7b49\u4e8e\u4e0a\u9762\u90a3\u4e2a\u503c\u7684\u7ec4\u4e2d\u6240\u6709\u5bf9\u8c61\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u51c6\u5907\u6b65\u9aa4\u662f\u8981\u6839\u636e\u6307\u5b9a\u7684\u5b57\u6bb5\u5c06\u6570\u636e\u6392\u5e8f\u3002\n\u56e0\u4e3a groupby() \u4ec5\u4ec5\u68c0\u67e5\u8fde\u7eed\u7684\u5143\u7d20\uff0c\u5982\u679c\u4e8b\u5148\u5e76\u6ca1\u6709\u6392\u5e8f\u5b8c\u6210\u7684\u8bdd\uff0c\u5206\u7ec4\u51fd\u6570\u5c06\u5f97\u4e0d\u5230\u60f3\u8981\u7684\u7ed3\u679c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u4ec5\u4ec5\u53ea\u662f\u60f3\u6839\u636e date \u5b57\u6bb5\u5c06\u6570\u636e\u5206\u7ec4\u5230\u4e00\u4e2a\u5927\u7684\u6570\u636e\u7ed3\u6784\u4e2d\u53bb\uff0c\u5e76\u4e14\u5141\u8bb8\u968f\u673a\u8bbf\u95ee\uff0c\n\u90a3\u4e48\u4f60\u6700\u597d\u4f7f\u7528 defaultdict() \u6765\u6784\u5efa\u4e00\u4e2a\u591a\u503c\u5b57\u5178\uff0c\u5173\u4e8e\u591a\u503c\u5b57\u5178\u5df2\u7ecf\u5728 1.6 \u5c0f\u8282\u6709\u8fc7\u8be6\u7ec6\u7684\u4ecb\u7ecd\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from collections import defaultdict\nrows_by_date = defaultdict(list)\nfor row in rows:\n    rows_by_date[row['date']].append(row)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u6837\u7684\u8bdd\u4f60\u53ef\u4ee5\u5f88\u8f7b\u677e\u7684\u5c31\u80fd\u5bf9\u6bcf\u4e2a\u6307\u5b9a\u65e5\u671f\u8bbf\u95ee\u5bf9\u5e94\u7684\u8bb0\u5f55\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "for r in rows_by_date['07/01/2012']:\nprint(r)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u4e0a\u9762\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6ca1\u6709\u5fc5\u8981\u5148\u5c06\u8bb0\u5f55\u6392\u5e8f\u3002\u56e0\u6b64\uff0c\u5982\u679c\u5bf9\u5185\u5b58\u5360\u7528\u4e0d\u662f\u5f88\u5173\u5fc3\uff0c\n\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u6bd4\u5148\u6392\u5e8f\u7136\u540e\u518d\u901a\u8fc7 groupby() \u51fd\u6570\u8fed\u4ee3\u7684\u65b9\u5f0f\u8fd0\u884c\u5f97\u5feb\u4e00\u4e9b\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}